<!--
    JBoss, Home of Professional Open Source
    Copyright 2015, Red Hat, Inc. and/or its affiliates, and individual
    contributors by the @authors tag. See the copyright.txt in the
    distribution for a full listing of individual contributors.

    Licensed under the Apache License, Version 2.0 (the "License");
    you may not use this file except in compliance with the License.
    You may obtain a copy of the License at
    http://www.apache.org/licenses/LICENSE-2.0
    Unless required by applicable law or agreed to in writing, software
    distributed under the License is distributed on an "AS IS" BASIS,
    WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
    See the License for the specific language governing permissions and
    limitations under the License.
-->
<!DOCTYPE html>
<html>
  <head>
    <title>WebSocket Echo Relay</title>
    <style>
      body {
        font-family: Verdana, Geneva, sans-serif;
        font-size: 14px;
        color: black;
        background-color: white;
      }
      input {
        font-family: 'Courier New',Courier,'Lucida Sans Typewriter','Lucida Typewriter',monospace;
        font-size: 14px;
        color: black;
        border: 1px solid black;
      }
      textarea {
        white-space: pre;
        word-wrap: normal;
        overflow-x: scroll;
      }
      #title {
        padding-left: 10px;
        font-size: 16px;
      }
      #subtitle {
        padding-left: 10px;
        font-size: 10px;
        color: gray;
      }
      #log {
        border: 1px solid black;
        width: 850px;
        height: 400px;
        font-family: 'Courier New',Courier,'Lucida Sans Typewriter','Lucida Typewriter',monospace;
        font-size: 14px;
        font-weight: bold;
        color: black;
        margin-top: 10px;
      }
      #control {
        padding-top: 10px;
      }
      #message{
        width: 650px;
        margin-right: 10px;
        font-weight: bold;
        padding-left: 10px;
      }
      #send {
        width: 180px;
        font-weight: bold;
      }
    </style>
  </head>
  <body style="">
    <form>
      <div id="title">WebSocket Echo Relay</div>
      <div id="subtitle">(Browser Javascript WebSocket Client) &lt;-&gt; (JBoss EAP WebSocket Server Endpoint) &lt;-&gt; (JBoss EAP Websocket Client Endpoint) &lt;-&gt; (Remote WebSocket Echo Server)</div>
      <textarea id="log" disabled="disabled" wrap="hard" ></textarea>
      <div id="control">
        <input id="message" type="text"/>
        <input type="submit" id="send" value="Send"/>
      </div>
    </form>
    <script>
      var port = "";
      if (window.location.host.search(".rhcloud.com") > 0) {
        port = ":8000";
      }
      var url = 'ws://' + window.location.host + port + window.location.pathname + 'relay';
      log("Connecting to " + url);
      var ws = new WebSocket(url);
      ws.onerror = function(event) {
        log("Error: " + event);
      };
      ws.onclose = function(event){
       log("Closed: " + event.code + ":" + codes[event.code] + " " + event.reason);
      };
      ws.onmessage = function(message) {
        log("RECV: " + message.data);
      };
      document.getElementById("send").onclick = function(event) {
        event.preventDefault();
        var msg = document.getElementById("message").value;
        if (msg) {
          log("SEND: " + msg);
          ws.send(msg);
        }
      };
      function log(s) {
        var d = new Date();
        var timestamp = "[" + d.toTimeString().slice(0, 8) + "] ";
        var logElement = document.getElementById("log");
        logElement.value += timestamp + s + "\n";
        logElement.scrollTop = logElement.scrollHeight;
      }
      document.getElementById("message").focus();
      var codes = {1000:"Normal", 1001:"Going away", 1002:"Protocol error", 1003:"Unsupported", 1005:"No status", 1006:"Abnormal", 1009:"Too large"};
    </script>
  </body>
</html>
